package com.test.daidai.main;

import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.test.ddaidai.main_account.ApproveManagerActivity;
import com.test.ddaidai.main_account.BusyDetailActivity;
import com.test.ddaidai.main_account.InverstProjectActivity;
import com.test.ddaidai.main_account.ReceivePlainActivity;
import com.test.ddaidai.main_project.PullToRefreshView;
import com.test.ddaidai.main_project.PullToRefreshView.OnFooterRefreshListener;
import com.test.ddaidai.main_project.PullToRefreshView.OnHeaderRefreshListener;
import com.test.ddaidai_android.activity.R;

public class CenterAccountFragment extends Fragment implements OnHeaderRefreshListener,OnFooterRefreshListener,OnClickListener {

	Button btn_inverst_project,btn_plain_project,btn_detail_project;
	RelativeLayout account_rel_info;
	ImageView img_account_man,img_account_phone,img_account_message;
	PullToRefreshView mPullToRefreshView;

	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			switch (msg.what) {
			case PullToRefreshView.PULL_DOWN_STATE:
				mPullToRefreshView.onHeaderRefreshComplete("更新于:"+new Date().toLocaleString());
				//				list.clear();
				break;
			case PullToRefreshView.PULL_UP_STATE :
				mPullToRefreshView.onFooterRefreshComplete();
				break;
			}
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View v = inflater.inflate(R.layout.fragment_centeraccount_main, null);
		mPullToRefreshView = (PullToRefreshView)v.findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		mPullToRefreshView.setLastUpdated(new Date().toLocaleString());
		img_account_man = (ImageView) v.findViewById(R.id.img_account_man);
		img_account_man.setOnClickListener(this);
		img_account_phone = (ImageView) v.findViewById(R.id.img_account_phone);
		img_account_phone.setOnClickListener(this);
		img_account_message = (ImageView) v.findViewById(R.id.img_account_message);
		img_account_message.setOnClickListener(this);
		account_rel_info = (RelativeLayout) v.findViewById(R.id.account_rel_info);
		account_rel_info.setOnClickListener(this);
		btn_inverst_project = (Button) v.findViewById(R.id.btn_inverst_project);
		btn_inverst_project.setOnClickListener(this);
		btn_plain_project = (Button) v.findViewById(R.id.btn_plain_project);
		btn_plain_project.setOnClickListener(this);
		btn_detail_project = (Button) v.findViewById(R.id.btn_detail_project);
		btn_detail_project.setOnClickListener(this);
		
		return v;
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		loadData(PullToRefreshView.PULL_UP_STATE);

	}
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {

		loadData(PullToRefreshView.PULL_DOWN_STATE);

	}

	private void loadData(final int what) {
		// 这里模拟从服务器获取数据
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(700);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = handler.obtainMessage();
				msg.what = what;
				handler.sendMessage(msg);
			}
		}).start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.account_rel_info:
			getActivity().startActivity(new Intent(getActivity(),ApproveManagerActivity.class));
			break;
		case R.id.btn_inverst_project:
			getActivity().startActivity(new Intent(getActivity(),InverstProjectActivity.class));
			break;
		case R.id.btn_plain_project:
			getActivity().startActivity(new Intent(getActivity(),ReceivePlainActivity.class));
			break;
		case R.id.btn_detail_project:
			getActivity().startActivity(new Intent(getActivity(),BusyDetailActivity.class));
			break;
		case R.id.img_account_man:
			Toast.makeText(getActivity(), "来信息啦....", Toast.LENGTH_SHORT).show();
			break;
		case R.id.img_account_phone:
			Toast.makeText(getActivity(), "来信息啦....", Toast.LENGTH_SHORT).show();
			break;
		case R.id.img_account_message:
			Toast.makeText(getActivity(), "来信息啦....", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}


}
